import 'package:flutter/material.dart';
import 'package:webviewx/webviewx.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  WebViewController controller;
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: WillPopScope(
          child: Scaffold(
            appBar: AppBar(
              title: const Text('Plugin example app'),
            ),
            body: Column(
              children: <Widget>[
                // Center(
                //   child: GestureDetector(
                //     onTap: () {},
                //     child: Text('show Confirm'),
                //   ),
                // ),
                Expanded(
                    child: WebViewx(
                        initialUrl: 'http://192.168.193.122:8086/#/',
                        javascriptChannels: <JavascriptChannel>{
                          JavascriptChannel(
                              name: "MY",
                              onMessageReceived: (JavascriptMessage message) {
                                print('##${message.message}');
                              })
                        },
                        appendUserAgent: "xiaoxiong",
                        loadFailWidget: Text('加载错误'),
                        progressBackground: Colors.greenAccent,
                        progressColor: Colors.red,
                        progressHeight: 1.5,
                        onWebViewCreated: (WebViewController controller) {
                          this.controller = controller;
                        },
                        navigationDelegate: (NavigationRequest request) async {
                          return NavigationDecision.navigate;
                        })),
              ],
            ),
          ),
          onWillPop: () async {
            var can = await controller.canGoBack();
            if (can) {
              controller.goBack();
            }
            return false;
          }),
    );
  }
}
